package algorithm;

import java.util.Arrays;

/**
 * 棋盘覆盖问题
 * User: Asuka
 * Date: 11-3-16
 * Time: 下午10:00
 */
public class ChessBoardCover {

    private static final int ROWSUM = 4;
    private static int[][] chessBoard = new int[ROWSUM][ROWSUM];
    private static int division = 1;

    public static void main(String[] args) {
        new ChessBoardCover().cover(0, 1);
        System.out.println(Arrays.deepToString(chessBoard));
    }

    private void cover(int specialX, int specialY) {
        cover(0, 0, specialX, specialY, ROWSUM);
    }

    /*
    private void cover(int chessBoardX, int chessBoardY, int specialX, int specialY, int size) {
        System.out.println(Arrays.deepToString(chessBoard));
        if (size == 1)
            return;
        size = size / 2;
        int d = division++;
        //左上角
        if (specialX < chessBoardX + size && specialY < chessBoardY + size) {
            cover(chessBoardX, chessBoardY, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size - 1][chessBoardY + size - 1] = d;
            cover(chessBoardX, chessBoardY, chessBoardX + size - 1, chessBoardY + size - 1, size);
        }
        //右上角

        if (specialX < chessBoardX + size && specialY >= chessBoardY + size) {
            cover(chessBoardX, chessBoardY + size, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size - 1][chessBoardY + size] = d;
            cover(chessBoardX, chessBoardY + size, chessBoardX + size - 1, chessBoardY + size, size);
        }
        //左下角
        if (specialX >= chessBoardX + size && specialY < chessBoardY + size) {
            cover(chessBoardX + size, chessBoardY, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size][chessBoardY + size - 1] = d;
            cover(chessBoardX + size, chessBoardY, chessBoardX + size, chessBoardY + size - 1, size);
        }

        //右下角
        if (specialX >= chessBoardX + size && specialY >= chessBoardY + size) {
            cover(chessBoardX + size, chessBoardY + size, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size][chessBoardY + size] = d;
            cover(chessBoardX + size, chessBoardY + size, chessBoardX + size, chessBoardY + size, size);
        }
    }
    */

    private void cover(int chessBoardX, int chessBoardY, int specialX, int specialY, int size) {
        System.out.println(Arrays.deepToString(chessBoard));
        if (size == 1)
            return;
        size = size / 2;
        //左上角
        if (specialX < chessBoardX + size && specialY < chessBoardY + size) {
            cover(chessBoardX, chessBoardY, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size - 1][chessBoardY + size - 1] = division;
            division++;
            cover(chessBoardX, chessBoardY, chessBoardX + size - 1, chessBoardY + size - 1, size);
        }
        //右上角
        if (specialX < chessBoardX + size && specialY >= chessBoardY + size) {
            cover(chessBoardX, chessBoardY + size, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size - 1][chessBoardY + size] = division;
            division++;
            cover(chessBoardX, chessBoardY + size, chessBoardX + size - 1, chessBoardY + size, size);
        }
        //左下角
        if (specialX >= chessBoardX + size && specialY < chessBoardY + size) {
            cover(chessBoardX + size, chessBoardY, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size][chessBoardY + size - 1] = division;
            division++;
            cover(chessBoardX + size, chessBoardY, chessBoardX + size, chessBoardY + size - 1, size);
        }
        //右下角
        if (specialX >= chessBoardX + size && specialY >= chessBoardY + size) {
            cover(chessBoardX + size, chessBoardY + size, specialX, specialY, size);
        } else {
            chessBoard[chessBoardX + size][chessBoardY + size] = division;
            division++;
            cover(chessBoardX + size, chessBoardY + size, chessBoardX + size, chessBoardY + size, size);
        }
    }
}
